package com.leviassaf.androidbasics;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class MyTextToSpeech extends Activity implements OnInitListener,
		OnClickListener, OnItemSelectedListener {

	private TextToSpeech tts;
	Button btnSpeak;
	TextView txtvwTranslatedText;
	EditText edtUserText;
	Spinner spinner;
	private String[] languages;
	private List<String> listStrLanguages = new ArrayList<String>();
	private ArrayAdapter<String> languagesAdapter;
	private String curLang;
	String strTranslatedText = "";
	Language languageTarget = Language.ENGLISH;
	String strUserText = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.texttospeech);
		languages = getResources().getStringArray(R.array.arrStrlanguages);

		for (int i = 0; i < languages.length; i++) {
			listStrLanguages.add(languages[i]);
		}
		setJavaRefs();
		languagesAdapter = new ArrayAdapter<String>(MyTextToSpeech.this,
				android.R.layout.simple_spinner_dropdown_item, listStrLanguages);
		languagesAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(languagesAdapter);

		this.setTitle(String.format("%s-%s", this.getTitle().toString(), this
				.getClass().getSimpleName()));

		tts = new TextToSpeech(this, this);
	}

	private void setJavaRefs() {
		// TODO Auto-generated method stub
		txtvwTranslatedText = (TextView) findViewById(R.id.txtvwTranslatedText);
		edtUserText = (EditText) findViewById(R.id.edtUserText);
		spinner = (Spinner) findViewById(R.id.spinner);
		btnSpeak = (Button) findViewById(R.id.btnSpeak);
		btnSpeak.setOnClickListener(this);
		spinner.setOnItemSelectedListener(this);
	}

	public String translate(String text) throws Exception {
		// Set the Client ID / Client Secret once per JVM. It is set statically
		// and applies to all services
		Translate.setClientId("leviassaf");
		Translate.setClientSecret("GA8cuCD6tsiPVCw9kD4CNyjHdomqAizHEi/vNceiojc=");

		strTranslatedText = Translate.execute(text, languageTarget);

		return strTranslatedText;
	}

	@Override
	public void onInit(int status) {
		// TODO Auto-generated method stub
		if (status == TextToSpeech.SUCCESS) {

			int result = SetLanguage(curLang);

			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Log.e("TTS", "This Language is not supported");
			} else {

				// speakOut("Ich");
			}

		} else {
			Log.e("TTS", "Initilization Failed!");
		}

	}

	private void speakOut(String text) {
		tts.speak(strTranslatedText, TextToSpeech.QUEUE_FLUSH, null);
	}

	private int SetLanguage(String lang) {
		int result = 0;
		if (lang.equals("French")) {
			result = tts.setLanguage(Locale.FRENCH);
			languageTarget = Language.FRENCH;
		} else if (lang.equals("German")) {
			result = tts.setLanguage(Locale.GERMAN);
			languageTarget = Language.GERMAN;
		} else if (lang.equals("Italian")) {
			result = tts.setLanguage(Locale.ITALIAN);
			languageTarget = Language.ITALIAN;
		} else if (lang.equals("Japanese")) {
			result = tts.setLanguage(Locale.JAPANESE);
			languageTarget = Language.JAPANESE;
		} else if (lang.equals("Korean")) {
			result = tts.setLanguage(Locale.KOREAN);
			languageTarget = Language.KOREAN;
		} else if (lang.equals("Chinese")) {
			result = tts.setLanguage(Locale.SIMPLIFIED_CHINESE);
			languageTarget = Language.CHINESE_SIMPLIFIED;
		}

		return result;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		SetLanguage(curLang);

		strUserText = edtUserText.getText().toString();

		class bgStuff extends AsyncTask<String, String, String> {
			@Override
			protected String doInBackground(String... params) {
				publishProgress("Translating...");
				// TODO Auto-generated method stub
				try {
					strTranslatedText = translate(strUserText);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					strTranslatedText = e.toString();
				}

				return null;
			}

			protected void onProgressUpdate(String... values) {
				// TODO Auto-generated method stub
				txtvwTranslatedText.setText(values[0]);
			}

			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				txtvwTranslatedText.setText(strTranslatedText);
				if (!strTranslatedText.contentEquals("")) {
					speakOut(strTranslatedText);
				}
			}

		}

		new bgStuff().execute();
	}

	@Override
	public void onItemSelected(AdapterView<?> adapter, View view, int position,
			long id) {
		curLang = (String) spinner.getAdapter().getItem((int) id);
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
	}

}
